<?php                                           
chdir(dirname(__FILE__)."/../..");
$path = getcwd();
chdir("../");
$path3 = getcwd();
chdir(dirname(__FILE__));
#debug($path3 , $path2);
include_once $path3."/lib/core.class.php";

class DiamondSelect extends IPlugin {
        var $color;

        function DiamondSelect($parent = null) {
                IPlugin :: IPlugin($parent);
                $this->tplfolder = dirname(__FILE__)."/tpl";
                $this->color = "blue";
        }

        function getPluginTag() {
                return "diamondselect";
        }
        function getPluginName() {
                return "Diamond Select management Plugin";
        }
        function getPluginVersion() {
                return "1.0.0";
        }

        function getProperty($p = null) {
                if (null == $p) {
                        return array ("MenuTitle", "MenuBlockTitle");
                }
                $result = "";
                switch ($p) {
                        case "MenuBlockTitle" :
                                {
                                        return "Diamond Select";
                                }
                                break;
                        case "MenuTitle" :
                                {
                                        return "Diamond Select";
                                }
                                break;
                };
                return $result;
        }

        // ----------------------------------------------------------------------------------
        // --- [ Handling all actions ] -----------------------------------------------------
        // ----------------------------------------------------------------------------------

        function doAction($params) {
            
        $core = getCoreInstance();
        $db = $core->getProperty("db");
        //$render = $core->framework->smarty;
                if (null != ($result = IPlugin :: doAction($params))) {
                        return $result;
                }
                $arg = getCoreInstance();
                $arg = $arg->getGET();
        
            
    ////////////////////////////////////////////////////////////  
            if ("change_select" == $_POST["action"])  { 
                #debug($_POST['select_id']); die();
                $_SESSION['admin_cfg']['current_select_id'] = $_POST['select_id'];
            }
    ////////////////////////////////////////////////////////////            
            if ("add" == $_POST["action"])  { 
                $sid = $_POST['select_id'];
                if( empty($sid) || !is_numeric($sid) || $sid<=0){
                    redirect("?".$this->getURL());
                }
                $sql_insert = "insert into d_select_vars (`select_id`, `var_key`)
                                   values ('$sid', '".str2db($_POST['var_key'])."' )";
                $db->Exec($sql_insert);
                redirect("?".$this->getURL());
            }
    ////////////////////////////////////////////////////////////            
     if ("edit" == $_POST["action"])  { 
                $sid = $_POST['select_id'];
                $kid = $_POST['id'];
                if( empty($sid) || !is_numeric($sid) || $sid<=0 || empty($kid) || !is_numeric($kid) || $kid<=0){
                    redirect("?".$this->getURL());
                }
                $sql_insert = "update d_select_vars set `var_key`='".str2db($_POST['var_key'])."' where var_id=$kid";
                
                $db->Exec($sql_insert);
                redirect("?".$this->getURL());
            }
    //////////////////////////////////////////////////////////// 
                   
            //doDeleteCatalog
            if ("doDeleteSelect" == $_GET['action']) {
                    $uid = $_GET['id'];
                    if (empty ($uid) || $uid < 0 || !is_numeric($uid)) {
                            redirect("?".$this->getURL());
                    }
                    $core = getCoreInstance();
                    $db = $core->getProperty("db");
    
                    $sql = "delete from d_select_vars where var_id=".str2db($uid)."";
                    $db->Exec($sql);
            }# do Delete Catalog
        }

        // ----------------------------------------------------------------------------------
        // --- [ Render pages ] -------------------------------------------------------------
        // ----------------------------------------------------------------------------------

        function Render($params) {
                global $_G;
                if (null != ($result = IPlugin :: Render($params))) {
                        return $result;
                }

                $core = getCoreInstance();
                $render = $core->framework->smarty;
                $render->assign("baseurl", "?".$this->getURL()."&page=".$_G["page"]);

                $page = "";

                switch ($_G["page"]) {

                        case "edit" :
                                $page = $this->renderProductAdd($render);
                                break;
                
                        case "add" :
                                $page = $this->renderProductAdd($render);
                                break;
                        default :
                                $page = $this->renderProductList($render);
                                break;
                }

                return $render->fetch("file:".$this->tplfolder."/".$page);
        }

        // ----------------------------------------------------------------------------------
        // --- [ Generate CProduct List ] --------------------------------------------------
        // ----------------------------------------------------------------------------------

        function cb_renderCT($data) {
                $dt = array (
                    array ($data["var_key"], "align=\"center\""),
                    array ("<a href=\"?".$this->getURL()."&page=edit&id={$data[var_id]}&st_paginator_page=".$st_paginator_page."&st_paginator_ppv=".$st_paginator_ppv."\">edit</a>&nbsp;
                    <a href=\"?".$this->getURL()."&action=doDeleteSelect&id={$data[var_id]}&st_paginator_page=".$st_paginator_page."&st_paginator_ppv=".$st_paginator_ppv."\">delete</a>", "align=\"center\""));
                return $dt;
        }



        function renderProductList(& $render) {
                $core = & getCoreInstance();
                $db = $core->getProperty("db");

                $curent_select_id = $_SESSION['admin_cfg']['current_select_id'];

                $pg = & $core->getSnippet("paginator");
                $tmp_url = $this->getURL()."&parent_id=".$curent_select_id;
                $page = $ppg = -1; /// curent count page
                        $params = array (
                                "cb_class" => & $this,
                                "cb_func" => cb_renderCT,
                                "urlbase" => $tmp_url,
                                "caption" => "Keys list",
                                "emptymsg" => "No Keys collected yet",
                                "header" => array ("Key",  "Action"),
                                "items" => "", "source" => "sql", // -- sql, array
                                "sql_items" => "select * from d_select_vars where select_id = '".$curent_select_id."' order by var_key",
                                "sql_count" => "select count(*) from d_select_vars where select_id = '".$curent_select_id."' ",
                                "ippg" => 15, // -- items per page
                                "nppv" => 5, // -- number pages per view
                                "page" => & $page,
                                "ppg" => & $ppg,
                                );
                $HTML = $pg->Render($params);
                $render->assign("productlist", $HTML);

                #select list
                $sql =" select * from d_selects";
                $res = $db->getAll($sql);
                $render->assign("selects", $res);
                

                $st_paginator_page = $_REQUEST['st_paginator_page'];
                $st_paginator_ppv = $_REQUEST['st_paginator_ppv'];

                $render->assign("add_url", "?".$this->getURL()."&select_id=".$curent_select_id."&page=add&st_paginator_page=".$st_paginator_page."&st_paginator_ppv=".$st_paginator_ppv);
                $render->assign("url_location", "?".$this->getURL());
                $render->assign("cur_select", $curent_select_id);

                //       Sibling
                #$render->assign("folder_locations", $res);


                $render->assign('action_url', "?".$this->getURL()."&parent_id=".$curent_select_id);

                $render->assign("check_valid", "?".$this->getURL()."&page=checkvalid");

                
                return "diamondselect.list.tpl.php";

        } /// renderProductList


// ----------------------------------------------------------------------------------
// --- [ Generate Product Edit page ] --------------------------------------------------
// ----------------------------------------------------------------------------------


        function renderProductEdit(& $render) {
         
                $core = & getCoreInstance();
                $db = $core->getProperty("db");
        }
// ----------------------------------------------------------------------------------
// --- [ Render Check ] --------------------------------------------------
// ----------------------------------------------------------------------------------


        function renderCheck(& $render) {

                $render->assign("check_valid", "?".$this->getURL()."&page=checkvalid");

                return "check.tpl.php";
        }
    

// ----------------------------------------------------------------------------------
// --- [ Generate Product Add Page ] --------------------------------------------------
// ----------------------------------------------------------------------------------


        function renderProductAdd(& $render) {

            
            $core = getCoreInstance();
            $db = $core->getProperty("db");
             
            $parent_id=$_REQUEST[parent_id];
                    
            $st_paginator_page = $_REQUEST['st_paginator_page'];
            $st_paginator_ppv = $_REQUEST['st_paginator_ppv'];
             
             
            $render->assign("tab", 1);       
            $render->assign("title", 'Add var to Select'."<br/>");
            
            $sid = $_SESSION['admin_cfg']['current_select_id'];
            #debug($sid , "adds");die();
            if( $sid==0 || !is_numeric($sid)){
                redirect("?".$this->getURL());
            }# if there was noo select selected
            $render->assign("select_id" , $sid);
            $render->assign("url", "?".$this->getURL());
            
            $kid = $_GET['id'];
            if( empty($kid) || $kid==0 || !is_numeric($kid)  ){
                #add
                $sql = "select * from d_selects where select_id =$sid" ;
                $res = $db->getAll($sql );
                $render->assign("select_name",$res[0]['select_name']);
                
                $render->assign("action","add");
            }else {
                #edit
                $sql = "select sv.*,ds.select_name from d_select_vars sv left join d_selects ds on sv.select_id=ds.select_id where sv.var_id =$kid" ;
                $res = $db->getAll($sql );
                #debug($res , "res");
                $render->assign("item",$res[0]);
                $render->assign("select_name",$res[0]['select_name']);
                
                $render->assign("action","edit");
                
            }

    
    

        
                return "diamond_select.edit.tpl.php";
        } // renderCatalogAdd
    
  
}
?>